package com.base.animation.model

import android.graphics.Canvas
import android.graphics.PointF
import android.util.Log
import com.base.animation.helper.PathObjectDeal

/**
 * @author:zhouzechao
 * @date: 2020/12/9
 * description：绘制节点
 */
private const val TAG = "DrawObject"

class DrawObject {

    var animDraws: MutableMap<Int, MutableList<AnimDrawObject>> = mutableMapOf()

    var currencyPosition: Int = 0

    var status: Status = Status.INIT

    fun draw(canvas: Canvas, pathObjectDeal: PathObjectDeal) {
        Log.i(TAG, "draw animDraws:${animDraws.size}")
        if (currencyPosition >= animDraws.size - 1) {
            status = Status.STOP
            pathObjectDeal.animDrawObjects.remove(this)
            return
        } else {
            status = Status.START
            animDraws[currencyPosition]?.forEach { drawObject ->
                pathObjectDeal.getDisplayItem(drawObject.displayItemId)?.apply {
                    draw(
                        canvas, drawObject.point.x, drawObject.point.y, drawObject.alpha,
                        drawObject.scaleX, drawObject.scaleY, drawObject.rotation
                    )
                    pathObjectDeal.recycler(this)
                }
            }
            currencyPosition++
        }
    }
}

data class AnimDrawObject(
    var displayItemId: String,
    var point: PointF = PointF(0f, 0f),
    var alpha: Int = 100,
    var scaleX: Float = 1.0f,
    var scaleY: Float = 1.0f,
    var rotation: Float = 0.0f
)

/**
 * 转化
 */
fun PathObject.toAnimDrawObject(): AnimDrawObject {
    return AnimDrawObject(displayItemId, point, alpha, scaleX, scaleY, rotation)
}

enum class Status(val value: Int) {
    INIT(0),
    START(1),
    STOP(2)
}